﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
using EA_Application;
using AddIn.EA.Core.Management;
using AddIn.EA.Core.Interface;
using AddIn.EA.Core.Common;

namespace AddIn.EA.Core
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class ProxyClient : ServicedComponent
    {
        #region Fields
#if DEBUG
        private AddInManager _manager; // = AddInManager.Initialize();
#else
        private AddInManager _manager = AddInManager.Initialize();
#endif
        #endregion

        #region EA Events

        public string EA_Connect(Repository repository)
        {
            return string.Empty;
        }

        public void EA_Disconnect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        public object EA_GetMenuItems(Repository repository, string menuLocation, string menuName)
        {

#if DEBUG
            //while (!System.Diagnostics.Debugger.IsAttached)
            //    System.Threading.Thread.Sleep(1000);

            _manager = AddInManager.Initialize();
            if (menuName != "") // Para testar sub-menus 
                System.Diagnostics.Debug.Write(""); // Adicione um breakpoint aqui
#endif
            IAddInItem item = _manager.Root.FindItem(menuName);

            if (item != null && item.IsMenu)
                return item.ToArray();
            else
                return "";
        }

        public void EA_GetMenuState(Repository repository, string menuLocation, string menuName, string itemName, bool isEnabled, bool isChecked)
        {
            //isEnabled = IsProjectOpen(repository);

            //if (isEnabled)
            //{
            //    IAddInItem item = _manager.Root.FindItem(itemName);

            //    if (item != null && item.HasAddIn)
            //    {
            //        MenuLocation location = (MenuLocation)Enum.Parse(typeof(MenuLocation), menuLocation);
            //        isEnabled = item.AddIn.CheckState(repository, location);
            //    }
            //}
        }

        public void EA_MenuClick(Repository repository, string menuLocation, string menuName, string itemName)
        {
            if (IsProjectOpen(repository))
            {
                IAddInItem item = _manager.Root.FindItem(itemName);

                if (item != null && item.HasAddIn)
                {
                    MenuLocation location = (MenuLocation)Enum.Parse(typeof(MenuLocation), menuLocation);
                    if (item.AddIn.CheckState(repository, location))
                        item.AddIn.Execute(repository, location);
                }
            }
        }

        #endregion

        #region Private Methods

        private bool IsProjectOpen(Repository Repository)
        {
            try
            {
                Collection c = Repository.Models;
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }
}
